package com.weds.edpf.core.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.resp.JsonResult;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;


import com.weds.edpf.core.entity.NDeviceMonitorEntity;
import com.weds.edpf.core.service.NDeviceMonitorService;

/**
 * @Author wangjianqiang
 * @Description 设备实时监控
 * @Date 2020-06-12
 * @Version 实时监控列表接口，状态统计接口  wangjianqiang  2020/06/12
 */
@RestController
@RequestMapping(value = "/nDeviceMonitor")
@Api(value = "设备实时监控", description = "设备实时监控")
public class NDeviceMonitorController extends BaseController {
    @Autowired
    private NDeviceMonitorService nDeviceMonitorService;


    private Logger log = LogManager.getLogger();

    /**
     * 设备监控列表
     *
     * @param record
     * @param dlxList
     * @return
     */
    @Logs
    @ApiOperation(value = "设备监控列表", notes = "设备监控列表")
    @RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<NDeviceMonitorEntity>> selectListPageByEntity(BasePageSearch<NDeviceMonitorEntity> record, @RequestParam(value = "dlxList") List<Integer> dlxList) {
        setPageHelper(record);
        NDeviceMonitorEntity entity = record.getSearch();
        if (entity == null) {
            entity = new NDeviceMonitorEntity();
        }
        List<NDeviceMonitorEntity> list = nDeviceMonitorService.monitorList(dlxList);
        if (list == null) {
            list = new ArrayList<>();
        }
        return succMsgData(new BaseCommPager<NDeviceMonitorEntity>(list));
    }

    /**
     * 设备状态统计
     * @param allTypeDev
     * @param type
     * @return
     */
    @Logs
    @ApiOperation(value = "设备状态统计", notes = "设备状态统计")
    @RequestMapping(value = "/devState", method = RequestMethod.GET)
    public JsonResult<Object> devState(@RequestParam("allTypeDev") boolean allTypeDev, @RequestParam("type") Integer type) {


        if (!allTypeDev) {
            if (type != 1 && type != 2 && type != 3) {
                return failMsg("参数错误");
            }
            List<Integer> list = Arrays.asList(type);
            int onlineCount = nDeviceMonitorService.countOnlineOrDev(list, 1);
            int offlineCount = nDeviceMonitorService.countOnlineOrDev(list, 2);
            int updateCount = nDeviceMonitorService.countUpdate(list);
            int downDataCount = nDeviceMonitorService.countDownData(list);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("onlineCount", onlineCount);
            jsonObject.put("offlineCount", offlineCount);
            jsonObject.put("allCount", offlineCount + onlineCount);
            jsonObject.put("updateCount", updateCount);
            jsonObject.put("downDataCount", downDataCount);
            return succMsgData(jsonObject);
        }


        List<Integer> list = Arrays.asList(1, 2, 3);
        int onlineCount = nDeviceMonitorService.countOnlineOrDev(list, 1);
        int offlineCount = nDeviceMonitorService.countOnlineOrDev(list, 2);
        //消费机的
        int updateCount = nDeviceMonitorService.countUpdate(Arrays.asList(2));
        int downDataCount = nDeviceMonitorService.countDownData(Arrays.asList(2));
        //考勤机的
        int attendUpdateCount = nDeviceMonitorService.countUpdate(Arrays.asList(1));
        //门控的
        int doolControlCount = nDeviceMonitorService.countUpdate(Arrays.asList(3));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("onlineCount", onlineCount);
        jsonObject.put("offlineCount", offlineCount);
        jsonObject.put("allCount", offlineCount + onlineCount);
        jsonObject.put("updateCount", updateCount);
        jsonObject.put("downDataCount", downDataCount);
        jsonObject.put("attendUpdateCount", attendUpdateCount);
        jsonObject.put("doolControlUpdateCount", doolControlCount);
        return succMsgData(jsonObject);
    }

}